package org.bw.bookstore.book.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.bw.bookstore.book.mapper.BookDetailedMapper;
import org.bw.bookstore.book.mapper.MemberMapper;
import org.bw.bookstore.book.mapper.ShoppingCartMapper;
import org.bw.bookstore.book.service.ShoppingCartService;
import org.bw.bookstore.common.entity.TbBookDetailed;
import org.bw.bookstore.common.entity.TbShoppingRecord;
import org.bw.bookstore.common.entity.TbUserInfo;
import org.bw.bookstore.common.utils.ResponseResult;
import org.bw.bookstore.common.utils.StringTools;
import org.bw.bookstore.common.vo.ShoppingAndBook;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * @Author: 钟乐
 * @Date: 2020/10/22 15:22
 **/

@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Resource
    private ShoppingCartMapper shoppingRecordMapper;
    
    @Resource
    private MemberMapper memberMapper;

    @Resource
    private BookDetailedMapper bookDetailedMapper;


    /**
    　　* TODO  joinShoppingCart--加入购物车
    　　* @Param [userInfoCode, bookDetailedCode] 
    　　* @return com.bw.common.utils.ResponseResult
    　　* @author 钟乐
    　　* @date 2020/10/22 21:23 
    　　*/
    @Override
    public ResponseResult joinShoppingCart(TbShoppingRecord tbShoppingRecord) {
        ResponseResult result = ResponseResult.SUCCESS();
        HashMap<String, Object> resultMap = new HashMap<>();
        //1、先查询购物车有没有与用户,商品都相同的记录
        List<TbShoppingRecord> shoppingRecords = shoppingRecordMapper.selectList(
                new QueryWrapper<TbShoppingRecord>()
                        //判断用户是否相同
                        .eq("user_info_code", tbShoppingRecord.getUserInfoCode())
                        //判断商品是否相同
                        .eq("book_detailed_code", tbShoppingRecord.getBookDetailedCode())
                        .last("limit 1")
        );

        //2.返回结果
        if(shoppingRecords!=null&&shoppingRecords.size()>0){
            System.out.println(shoppingRecords);
            result.setMessage("亲，该书籍已被添加过");
            return result;
        }

        //3、没有该条记录就可以根据用户的选择进行添加
        TbShoppingRecord record = new TbShoppingRecord();
        record.setUserInfoCode(tbShoppingRecord.getUserInfoCode());
        record.setBookDetailedCode(tbShoppingRecord.getBookDetailedCode());
        record.setDeleted(0);
        record.setBookNum(tbShoppingRecord.getBookNum());
        record.setCreateTime(new Date());
        record.setCode(StringTools.getRandomString(1));
        System.out.println(record);

        shoppingRecordMapper.insert(record);

        //4、判断是否添加成功
        if(record.getId()!=null){
            resultMap.put("record", record);
            result.setResult(resultMap);
            result.setMessage("购物车添加成功");
            return result;
        }
        result.setMessage("购物车添加失败");
        return result;
    }

    /**
    　　* TODO  showShoppingCart--根据用户code查找购物车记录
    　　* @Param [userId]
    　　* @return com.bw.common.utils.ResponseResult
    　　* @author 钟乐
    　　* @date 2020/10/23 8:42
    　　*/
    @Override
    public ResponseResult showShoppingCart(String userInfoCode) {
        ResponseResult result = ResponseResult.SUCCESS();
        HashMap<String, Object> resultMap = new HashMap<>();
        ArrayList<TbBookDetailed> tbBookDetailedArrayList = new ArrayList<>();
        //1.判断用户是否存在
        TbUserInfo tbUserInfo = memberMapper.selectOne(
                new QueryWrapper<TbUserInfo>()
                .eq("code",userInfoCode)
        );
        if(tbUserInfo==null){
            result.setMessage("该用户未登录");
            return result;
        }

        //根据用户code查找购物车记录
        List<TbShoppingRecord> tbShoppingRecords = shoppingRecordMapper.showShoppingCart(userInfoCode);
        //然后遍历购物车记录去查询书籍详情
        for(TbShoppingRecord tbShoppingRecord:  tbShoppingRecords){
            TbBookDetailed tbBookDetailed = bookDetailedMapper.selectOne(
                    new QueryWrapper<TbBookDetailed>()
                            .lambda()
                            .eq(TbBookDetailed::getCode, tbShoppingRecord.getBookDetailedCode())
            );
            tbBookDetailedArrayList.add(tbBookDetailed);
            tbShoppingRecord.setBookDetailedList(tbBookDetailed);
        }

        resultMap.put("tbShoppingRecords",tbShoppingRecords);
        result.setResult(resultMap);
        return result;
    }

    /**
    　　* TODO  findShoppingRecordByCode--根据商品code精确查询
    　　* @Param [bookDetailedCode]
    　　* @return com.bw.common.utils.ResponseResult
    　　* @author 钟乐
    　　* @date 2020/10/23 21:46
    　　*/
    @Override
    public ResponseResult findShoppingRecordByCode(String userInfoCode,String bookDetailedCode) {
        ResponseResult result = ResponseResult.SUCCESS();
        HashMap<String, Object> resultMap = new HashMap<>();
        System.err.println(userInfoCode+"----"+bookDetailedCode);
        ShoppingAndBook shoppingAndBook = shoppingRecordMapper.findShoppingRecordByCode(userInfoCode, bookDetailedCode);
        System.out.println(shoppingAndBook);
        if (shoppingAndBook == null){
            return result.setFail("没有查到记录");
        }

        //赋值shopping
        TbShoppingRecord tbShoppingRecord = new TbShoppingRecord();
        tbShoppingRecord.setUserInfoCode(userInfoCode);
        tbShoppingRecord.setId(shoppingAndBook.getId());
        tbShoppingRecord.setBookNum(shoppingAndBook.getBookNum());
        tbShoppingRecord.setDeleted(shoppingAndBook.getDeleted());

        //赋值Book
        TbBookDetailed tbBookDetailed = new TbBookDetailed();
        tbBookDetailed.setBookName(shoppingAndBook.getBookName());
        tbBookDetailed.setBookPrice(shoppingAndBook.getBookPrice());
        tbBookDetailed.setCode(bookDetailedCode);
        tbShoppingRecord.setBookDetailedList(tbBookDetailed);

        System.err.println(tbShoppingRecord);
        resultMap.put("tbShoppingRecord",tbShoppingRecord);
        result.setResult(resultMap);
        return result;
    }


    /**
    　　* TODO  delShoppingCart--移除购物车记录
    　　* @Param [shoppingCartId]
    　　* @return com.bw.common.utils.ResponseResult
    　　* @author 钟乐
    　　* @date 2020/10/23 8:46
    　　*/
    @Override
    public ResponseResult delShoppingCart(String shoppingCartId) {
        ResponseResult result = ResponseResult.SUCCESS();
        HashMap<String, Object> resultMap = new HashMap<>();
        //1.根据shoppingCartId查询是否有该记录
        String[] split = shoppingCartId.split(",");
        for(String str:split){
            TbShoppingRecord shoppingRecord = shoppingRecordMapper.selectById(Integer.parseInt(str));
            if(shoppingRecord==null){
                result.setMessage("没有这条购物车记录");
                return result;
            }
            //2、判断是否删除
            if(!shoppingRecord.getDeleted().equals(0)){
                result.setMessage("该记录已被删除");
                return result;
            }
            //3.设置删除标识
            shoppingRecord.setDeleted(1);
            shoppingRecordMapper.updateById(shoppingRecord);
            resultMap.put("shoppingRecord",shoppingRecord);
            result.setResult(resultMap);
        }
        return result;
    }
}
